package com.example.admin.system.config;

import com.example.admin.system.bean.dto.ApiErrorResponseDTO;
import org.apache.tomcat.websocket.AuthenticationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

/**
 * @author liucheng
 * @since 0.1 2021-07-23 8:45 PM
 **/
@RestControllerAdvice
public class CustomRestControllerAdvice extends ResponseEntityExceptionHandler {
    @ExceptionHandler(Exception.class)
    public final ResponseEntity<ApiErrorResponseDTO> handleAllException(Exception ex)  {
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body((ApiErrorResponseDTO.builder()
                .errorCode(HttpStatus.INTERNAL_SERVER_ERROR.toString())
                .message(ex.getMessage()).build()));
    }

    @ExceptionHandler(AuthenticationException.class)
    public final ResponseEntity<ApiErrorResponseDTO> handleAuthenticationException(AuthenticationException ex)  {
        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body((ApiErrorResponseDTO.builder()
                .errorCode(HttpStatus.UNAUTHORIZED.toString())
                .message(ex.getMessage()).build()));
    }
}
